package com.zyk.scaffold.common.utils;

import com.zyk.scaffold.common.exception.BusinessIllegalArgumentException;
import org.apache.commons.collections4.MapUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

public class AssertUtil {
    /**
     * 抛出错误信息
     *
     * @param message
     */
    public static void error( String message) {
         throw new BusinessIllegalArgumentException(message);
    }

    public static void error(String message, Throwable cause) {
        throw new BusinessIllegalArgumentException(message, cause);
    }
    /**
     * 字符串参数不能为空
     *
     * @param str
     * @param message
     */
    public static void notEmptyString(String str, String message) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(str)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    /**
     * 判断是否为null
     * @param object
     * @param message
     */
    public static void notIsNull(Object object, String message) {
        if (null==object) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    /**
     * map验证是否为空
     * @param map
     * @param message
     */
    public static void notEmptyMap(Map<?, ?> map, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    /**
     * 验证集合是否为空
     * @param collection
     * @param message
     */
    public static void notEmptyCollection(Collection<?> collection, String message) {
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(collection)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void isEmptyCollection(Collection<?> collection, String message) {
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(collection)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void state(Boolean expression, String message) {
        if (Boolean.TRUE.equals(expression)) {
            return;
        }
        throw new BusinessIllegalArgumentException(message);
    }

    public static void state(Boolean expression, Supplier<String> messageSupplier) {
        if (Boolean.TRUE.equals(expression)) {
            return;
        }
        throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
    }

    @Deprecated
    public static void state(Boolean expression) {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

    public static void isTrue(Boolean expression, String message) {
        if (Boolean.TRUE.equals(expression)) {
            return;
        }
        throw new BusinessIllegalArgumentException(message);
    }
    public static void isFalse(Boolean expression, String message) {
        if (Boolean.FALSE.equals(expression)) {
            return;
        }
        throw new BusinessIllegalArgumentException(message);
    }

    public static void isTrue(Boolean expression, Supplier<String> messageSupplier) {
        if (Boolean.TRUE.equals(expression)) {
            return;
        }
        throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
    }

    @Deprecated
    public static void isTrue(Boolean expression) {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void isNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object != null) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void isNull(@Nullable Object object) {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object == null) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void notNull(@Nullable Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    public static void hasLength(@Nullable String text, String message) {
        if (!StringUtils.hasLength(text)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
        if (!StringUtils.hasLength(text)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void hasLength(@Nullable String text) {
        hasLength(text,
                "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    public static void hasText(@Nullable String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void hasText(@Nullable String text, Supplier<String> messageSupplier) {
        if (!StringUtils.hasText(text)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void hasText(@Nullable String text) {
        hasText(text,
                "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, String message) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, Supplier<String> messageSupplier) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void doesNotContain(@Nullable String textToSearch, String substring) {
        doesNotContain(textToSearch, substring,
                () -> "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
    }

    public static void notEmpty(@Nullable Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void notEmpty(@Nullable Object[] array, Supplier<String> messageSupplier) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void notEmpty(@Nullable Object[] array) {
        notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
    }

    public static void noNullElements(@Nullable Object[] array, String message) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new BusinessIllegalArgumentException(message);
                }
            }
        }
    }

    public static void noNullElements(@Nullable Object[] array, Supplier<String> messageSupplier) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
                }
            }
        }
    }

    @Deprecated
    public static void noNullElements(@Nullable Object[] array) {
        noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
    }

    public static void notEmpty(@Nullable Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void notEmpty(@Nullable Collection<?> collection) {
        notEmpty(collection,
                "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }

    public static void noNullElements(@Nullable Collection<?> collection, String message) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    throw new BusinessIllegalArgumentException(message);
                }
            }
        }
    }

    public static void noNullElements(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
                }
            }
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BusinessIllegalArgumentException(message);
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BusinessIllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Deprecated
    public static void notEmpty(@Nullable Map<?, ?> map) {
        notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

    public static void isInstanceOf(Class<?> type, @Nullable Object obj, String message) {
        notNull(type, "Type to check against must not be null");
        if (!type.isInstance(obj)) {
            instanceCheckFailed(type, obj, message);
        }
    }

    public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) {
        notNull(type, "Type to check against must not be null");
        if (!type.isInstance(obj)) {
            instanceCheckFailed(type, obj, nullSafeGet(messageSupplier));
        }
    }

    public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
        isInstanceOf(type, obj, "");
    }

    public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, String message) {
        notNull(superType, "Super type to check against must not be null");
        if (subType == null || !superType.isAssignableFrom(subType)) {
            assignableCheckFailed(superType, subType, message);
        }
    }

    public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, Supplier<String> messageSupplier) {
        notNull(superType, "Super type to check against must not be null");
        if (subType == null || !superType.isAssignableFrom(subType)) {
            assignableCheckFailed(superType, subType, nullSafeGet(messageSupplier));
        }
    }

    public static void isAssignable(Class<?> superType, Class<?> subType) {
        isAssignable(superType, subType, "");
    }


    private static void instanceCheckFailed(Class<?> type, @Nullable Object obj, @Nullable String msg) {
        String className = (obj != null ? obj.getClass().getName() : "null");
        String result = "";
        Boolean defaultMessage = true;
        if (StringUtils.hasLength(msg)) {
            if (endsWithSeparator(msg)) {
                result = msg + " ";
            }
            else {
                result = messageWithTypeName(msg, className);
                defaultMessage = false;
            }
        }
        if (defaultMessage) {
            result = result + ("Object of class [" + className + "] must be an instance of " + type);
        }
        throw new BusinessIllegalArgumentException(result);
    }

    private static void assignableCheckFailed(Class<?> superType, @Nullable Class<?> subType, @Nullable String msg) {
        String result = "";
        Boolean defaultMessage = true;
        if (StringUtils.hasLength(msg)) {
            if (endsWithSeparator(msg)) {
                result = msg + " ";
            }
            else {
                result = messageWithTypeName(msg, subType);
                defaultMessage = false;
            }
        }
        if (defaultMessage) {
            result = result + (subType + " is not assignable to " + superType);
        }
        throw new BusinessIllegalArgumentException(result);
    }

    private static Boolean endsWithSeparator(String msg) {
        return (msg.endsWith(":") || msg.endsWith(";") || msg.endsWith(",") || msg.endsWith("."));
    }

    private static String messageWithTypeName(String msg, @Nullable Object typeName) {
        return msg + (msg.endsWith(" ") ? "" : ": ") + typeName;
    }

    @Nullable
    private static String nullSafeGet(@Nullable Supplier<String> messageSupplier) {
        return (messageSupplier != null ? messageSupplier.get() : null);
    }
}
